﻿package com.PlantVsZombies.thread;

import java.util.ArrayList;
import java.util.List;

import com.PlantVsZombies.common.Life;

public class ThreadPool {
	
	private static ThreadPool instance = null;
	
	private List<Life> lifes;
	
	private int size;
	
	private ThreadPool(int size) {
		this.size = size;
		lifes = new ArrayList<Life>(size);
	}
	
	public static ThreadPool newInstance(int size){
		if (instance == null) {
			instance = new ThreadPool(size);
		}
		return instance;
	}
	
	public void remove(Life life) {
		if (lifes.contains(life)) {
			lifes.remove(life);
		}else {
			System.out.println("该对象不存在");
		}
	}
	
	public void push (Life life) {
		if (lifes.size() < size) {
			lifes.add(life);
		} else {
			for (Life lf : lifes) {
				if (lf != null && !lf.isLive()) {
					lf = life;
				}
			}
		}
	}
	
	public void executeRunnable() {
		for (Life life : lifes) {
			if (life.isLive() && !life.isAlive()) {
				life.start();
			}
		}
	}


	
}
